// 车型选择
(function() {
    // 车辆数据源接口
    var URL = {
        // brand
        brandsUrl: "http://www.cheyipai.com/SearchCars/GetAllBrands",
        // cars
        BrandIdUrl: "http://www.cheyipai.com/SearchCars/GetCarSeriesByBrandId",
        // model
        SeriesIdUrl: "http://www.cheyipai.com/SearchCars/GetCarModelsBySeriesId"
    }
    var J_carSelect = $(".nextPanel");
    var J_letterList = $(".nextPanel .letter-list");
    var J_brandList = $(".nextPanel .brand-list");
    var J_seriesList = $(".nextPanel .series-list");
    var J_modelsList = $(".nextPanel .models-list");
    var carBrand="";
    //$("#J_model").on("click", function() {
    $(".C_type").on("click", function() {
        //初始数据请求
        $.getJSON(URL.brandsUrl + "?callback=?", function(data) {
            if (data.ResCode === "0000") {
                var html = [],
                    arry = [];
                $.each(data.firstByts, function(k, v) {
                    html.push("<li data-anchor='" + v + "'>" + v + "</li>");
                });
                J_letterList.html(html);
                $.each(data.firstByts, function(a, b) {
                    arry.push("<li class='disactive' data-maoid='" + b + "'>" + b + "</li>");
                    $.each(data.brands, function(c, d) {
                        if (b === d.FirstByte) {
                            arry.push("<li data-BrandId='" + d.BrandID + "'>" + d.BrandName + "</li>");
                        }
                    });
                });
                J_brandList.html(arry);
                J_carSelect.fadeIn();
            }
        });
        return false;
    });
    J_modelsList.on("click", "li", function() {
        if (!$(this).hasClass("disactive")) {
            $(this).closest(".par").find(".C_type").val(carBrand+" "+$(this).text());
            J_carSelect.hide();
            J_letterList.add(J_brandList).add(J_seriesList).add(J_modelsList).empty();
        }
    });
    // 车系追加
    J_brandList.on("click", "li", function() {
        if (!$(this).hasClass("disactive")) {
            $.getJSON(URL.BrandIdUrl + "?callback=?&&brandId=" + $(this).attr("data-brandId"), function(data) {
                var html = [];
                $.each(data.Data, function(a, b) {
                    html.push("<li class='disactive'>" + a + "</li>");
                    $.each(b, function(k, v) {
                        html.push("<li data-seriesId='" + v.SeriesID + "'>" + v.SeriesName + "</li>");
                    });
                });
                J_seriesList.html(html);
            });
            $(this).addClass('selected').siblings().removeClass('selected');
            carBrand=$(this).text();
        }
        return false;
    });
    // 车型追加
    J_seriesList.on("click", "li", function() {
        if (!$(this).hasClass("disactive")) {
            $.getJSON(URL.SeriesIdUrl + "?callback=?&&seriesId=" + $(this).attr("data-seriesId"), function(data) {
                var html = [];
                var arr = [];
                var arry = [];
                $.each(data.Data, function(a, b) {
                    arr.push(b.ModelSuffix);
                });
                $.unique(arr);
                arr.sort();
                $.each(arr, function(a, b) {
                    arry.push("<li class='disactive'>" + b + "</li>");
                    $.each(data.Data, function(c, d) {
                        if (b === d.ModelSuffix) {
                            arry.push("<li data-carmodelid='" + d.CarModelID + "'>" + d.ModelName + "</li>");
                        }
                    });
                });
                J_modelsList.html(arry);
            });
            $(this).addClass('selected').siblings().removeClass('selected');
        }
        return false;
    });
    // 自定义锚点
    J_letterList.on("click", "li", function() {
        var toAttr = $(this).attr("data-anchor");
        J_brandList.find(".disactive").each(function(k, v) {
            if ($(v).attr("data-maoid") === toAttr) {
                var itp = v.offsetTop + 10;
                $(".data").animate({
                    scrollTop: itp
                }, 450);
                return false;
            }
        });
        $(this).addClass('selected').siblings().removeClass('selected');
        return false;
    });
    $(top.document).add($(document)).on("click", function() {
        J_carSelect.fadeOut();
    });
})();